<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      :title="props.title"
      :width="!props.isReadonly ? '550px' : '850px'"
      draggable
      @close="closeDialog"
      destroy-on-close
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="120"
        v-show="!props.isReadonly"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="问题分类" prop="problemTypeCode">
              <el-select
                v-if="!props.isReadonly"
                v-model="formData.problemTypeCode"
                placeholder="请选择问题分类"
                clearable
                style="width: 100%"
                @change="problemTypeChange"
              >
                <el-option
                  v-for="item in dictionaryStore.filterDictList(
                    'ExceptionType'
                  )"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)"
                />
              </el-select>
              <span v-else>{{ formData.problemTypeCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="问题二级分类" prop="exceptionTypeSubId">
              <el-select
                v-if="!props.isReadonly"
                v-model="formData.exceptionTypeSubId"
                placeholder="请选择问题二级分类"
                clearable
                style="width: 100%"
                @change="problemTypeSubChange"
              >
                <el-option
                  v-for="item in towProblemApproachList"
                  :key="item.id"
                  :label="item.exceptionTypeSubCode"
                  :value="String(item.id)"
                />
              </el-select>
              <span v-else>{{ formData.problemTypeCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="问题级别" prop="exceptionLevelId">
              <el-select
                v-if="!props.isReadonly"
                v-model="formData.exceptionLevelId"
                placeholder="请选择问题级别"
                clearable
                style="width: 100%"
                @change="handleChangeProblemLevel"
              >
                <el-option
                  v-for="item in problemLevelList"
                  :key="item.id"
                  :label="item.exceptionLevelCode"
                  :value="item.id"
                />
              </el-select>
              <span v-else>{{ formData.exceptionLevelCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="责任部门" prop="departmentId">
              <el-tree-select
                v-if="!props.isReadonly"
                placeholder="请选择责任部门"
                v-model="formData.departmentId"
                :data="organizationList"
                :render-after-expand="false"
                :check-strictly="true"
                node-key="id"
                :props="{
                  label: 'organizationCode',
                  value: 'id',
                  children: 'nodes',
                }"
                style="width: 100%"
                @node-click="handleChangeDepartment"
                clearable
              />
              <span v-else>{{ formData.departmentId }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="问题处理方式" prop="problemApproachType">
              <el-select
                v-if="!props.isReadonly"
                v-model="formData.problemApproachType"
                placeholder="请选择问题处理方式"
                clearable
                style="width: 100%"
                @change="handleChangeProblemApproachType"
              >
                <el-option
                  v-for="item in problemApproachTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="String(item.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="nextStep"
            >下一步</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <correctiveMeasure-edit
      :visible="addDialogMeasure.visible"
      :title="addDialogMeasure.title"
      :data="addDialogMeasure.data"
      :is-readonly="addDialogMeasure.readvisible"
      @change-visible1="changeVisibleMeasure"
      @backList="backList"
      @goback="goback"
    />
    <feedbackReport-edit
      :visible="addDialogFeed.visible"
      :title="addDialogFeed.title"
      :data="addDialogFeed.data"
      @change-visible2="changeVisibleFeed"
      @backList2="backList2"
      @goback2="goback2"
    />
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import dayjs from "dayjs";
// import html2pdf from "./PDF.js";
const dictionaryStore = useDictStoreHook();

function PDFprit() {
  const table = document.getElementById("table");
  // html2pdf(table, {
  //   background: "#fff",
  //   minimumUnit: "tr",
  //   orientation: "l",
  // });
  // html2pdf("table", "对账单", dayjs(new Date()).format("YYYY-mm-dd"));
}
defineOptions({
  name: "Q8DRELEASEITEMEDIT",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  qualityExceptionCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  productionTypeId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  productionTypeName: [
    // { required: true, message: "请选择产品类目名称", trigger: "change" },
  ],
  materialId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  figureNumber: [
    // { required: true, message: "请选择图号", trigger: "change" },
  ],
  qualityQuantity: [
    // { required: true, message: "请输入数量", trigger: "blur" },
  ],
  failureQuantity: [
    // { required: true, message: "请输入失效对应数量", trigger: "blur" },
  ],
  failureMode: [
    // { required: true, message: "请选择失效模式", trigger: "change" },
  ],
  occurrenceTime: [
    // { required: true, message: "请选择发生时间", trigger: "change" },
  ],
  occurrenceLocation: [
    // { required: true, message: "请选择发生地点", trigger: "change" },
  ],
  informationSourceId: [
    // { required: true, message: "请选择信息来源", trigger: "change" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  informationSourceContent: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  exceptionState: [],
  isInternal: [],
  supplyId: [
    // { required: true, message: "请选择生产单位", trigger: "change" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  supplyCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  supplyName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  problemTypeCode: [
    { required: true, message: "请选择问题分类", trigger: "change" },
  ],
  lineProcessName: [
    // { required: true, message: "请输入生产线/过程", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  productProcess: [
    // { required: true, message: "请选择产品/过程状态", trigger: "change" },
  ],
  problemFrequency: [
    // { required: true, message: "请输入问题发生频度", trigger: "blur" },
  ],
  departmentId: [
    { required: true, message: "请选择责任部门", trigger: "change" },
  ],
  targetTime: [
    // { required: true, message: "请选择责任部门", trigger: "change" },
  ],
  exceptionTypeSubId: [
    { required: true, message: "请选择问题二级分类", trigger: "change" },
  ],
  exceptionLevelId: [
    { required: true, message: "请选择问题级别", trigger: "change" },
  ],
  problemApproachType: [
    { required: true, message: "请选择问题处理方式", trigger: "change" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  qualityProblemCode: "",
  qualityExceptionId: "",
  qualityExceptionCode: "",
  informationSourceId: "",
  lineProcessName: "",
  productProjectName: "",
  sortCodeId: "",
  sortCode: "",
  productProcess: "",
  failureMode: "",
  consequenceInfluence: "",
  problemFrequency: "",
  proposeDepartmentId: "",
  proposeDepartmentCode: "",
  proposeDepartmentName: "",
  proposeManagerId: "",
  proposeManagerName: "",
  departmentId: "",
  departmentCode: "",
  departmentName: "",
  departmentManagerId: "",
  departmentManagerName: "",
  isInternal: 1,
  supplyId: "",
  supplyCode: "",
  supplyName: "",
  problemTime: "",
  problemUserId: "",
  problemUserName: "",
  problemTypeCode: "",
  exceptionTypeSubId: "",
  exceptionTypeSubCode: "",
  exceptionTypeSubName: "",
  exceptionLevelId: "",
  problemApproachId: "",
  problemApproachCode: "",
  problemApproachType: "",
  problemApproachName: "",
  releaseState: "",
  auditUserId: "",
  auditUserName: "",
  provisionalMeasuresDeadline: "",
  rectificationDeadline: "",
  rectificationRequirement: "",
  agreeWith: "",
  approvalOpinion: "",
  approvalTime: "",
  targetTime: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
  // 涉及数量
  qualityQuantity: 1,
  // 失效模式
  failureModeContent: "",
  // 发生地点
  occurrenceLocation: "",
  // 状态
  productProcessContent: "",
  sortName: "",
  // 图号编号
  materialProcessCode: "",
  materialProcessName: "",
  materialProcessId: "",
  materialProcessFigure: "",
  // 问题处理方式
  exceptionSolvingType: "",
  // 问题级别
  exceptionLevelCode: "",
  exceptionLevel: "",
  exceptionLevelContent: "",
  // 8D问题反馈报告
  q8dExceptionAssignDTO: {
    id: "",
    qualityExceptionId: "",
    qualityExceptionCode: "",
    exceptionAssignCode: "",
    customerComplaintCode: "",
    startDate: "",
    contentTypeId: "",
    contentType: "",
    propertySortId: "",
    propertySort: "",
    supplyId: "",
    supplyCode: "",
    supplyName: "",
    materialId: "",
    materialCode: "",
    materialName: "",
    figureNumber: "",
    exceptionQuantity: 1,
    exceptionDescription: "",
    exceptionAssignState: "",
    assetPhotos: [],
  },
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        qualityProblemCode,
        qualityExceptionId,
        qualityExceptionCode,
        informationSourceId,
        lineProcessName,
        productProjectName,
        sortCodeId,
        sortCode,
        productProcess,
        failureMode,
        consequenceInfluence,
        problemFrequency,
        proposeDepartmentId,
        proposeDepartmentCode,
        proposeDepartmentName,
        proposeManagerId,
        proposeManagerName,
        departmentId,
        departmentCode,
        departmentName,
        departmentManagerId,
        departmentManagerName,
        isInternal,
        supplyId,
        supplyCode,
        supplyName,
        problemTime,
        problemUserId,
        problemUserName,
        problemTypeCode,
        exceptionTypeSubId,
        exceptionTypeSubCode,
        exceptionTypeSubName,
        exceptionLevelId,
        problemApproachId,
        problemApproachCode,
        problemApproachType,
        problemApproachName,
        releaseState,
        auditUserId,
        auditUserName,
        provisionalMeasuresDeadline,
        rectificationDeadline,
        rectificationRequirement,
        agreeWith,
        approvalOpinion,
        approvalTime,
        targetTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        qualityQuantity,
        failureModeContent,
        occurrenceLocation,
        productProcessContent,
        sortName,
        // 图号编号
        materialProcessCode,
        materialProcessName,
        materialProcessId,
        materialProcessFigure,
        exceptionSolvingType,
        exceptionLevelCode,
        exceptionLevel,
        exceptionLevelContent,
        q8dExceptionAssignDTO,
      } = newVal;
      Object.assign(formData, {
        id,
        qualityProblemCode,
        qualityExceptionId,
        qualityExceptionCode,
        informationSourceId,
        lineProcessName,
        productProjectName,
        sortCodeId,
        sortCode,
        productProcess,
        failureMode,
        consequenceInfluence,
        problemFrequency,
        proposeDepartmentId,
        proposeDepartmentCode,
        proposeDepartmentName,
        proposeManagerId,
        proposeManagerName,
        departmentId,
        departmentCode,
        departmentName,
        departmentManagerId,
        departmentManagerName,
        isInternal,
        supplyId,
        supplyCode,
        supplyName,
        problemTime,
        problemUserId,
        problemUserName,
        problemTypeCode,
        exceptionTypeSubId,
        exceptionTypeSubCode,
        exceptionTypeSubName,
        exceptionLevelId,
        problemApproachId,
        problemApproachCode,
        problemApproachType,
        problemApproachName,
        releaseState,
        auditUserId,
        auditUserName,
        provisionalMeasuresDeadline,
        rectificationDeadline,
        rectificationRequirement,
        agreeWith,
        approvalOpinion,
        approvalTime,
        targetTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        qualityQuantity,
        failureModeContent,
        occurrenceLocation,
        productProcessContent,
        sortName,
        materialProcessCode,
        materialProcessName,
        materialProcessId,
        materialProcessFigure,
        exceptionSolvingType,
        exceptionLevelCode,
        exceptionLevel,
        exceptionLevelContent,
        q8dExceptionAssignDTO,
      });
      formData.problemApproachType = "";
      if (formData.problemTypeCode == "1") {
        problemApproachTypeList.value = [{ value: "0", label: "预防纠正措施" }];
      } else {
        problemApproachTypeList.value = [
          { value: "1", label: "简单问题处理" },
          { value: "2", label: "问题8D处理" },
        ];
      }
      getTowProblemApproachList();
      console.log("formData", formData);
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.qualityProblemCode = "";
  formData.qualityExceptionId = "";
  formData.qualityExceptionCode = "";
  formData.informationSourceId = "";
  formData.lineProcessName = "";
  formData.productProjectName = "";
  formData.sortCodeId = "";
  formData.sortCode = "";
  formData.productProcess = "";
  formData.failureMode = "";
  formData.consequenceInfluence = "";
  formData.problemFrequency = "";
  formData.proposeDepartmentId = "";
  formData.proposeDepartmentCode = "";
  formData.proposeDepartmentName = "";
  formData.proposeManagerId = "";
  formData.proposeManagerName = "";
  formData.departmentId = "";
  formData.departmentCode = "";
  formData.departmentName = "";
  formData.departmentManagerId = "";
  formData.departmentManagerName = "";
  formData.isInternal = 1;
  formData.supplyId = "";
  formData.supplyCode = "";
  formData.supplyName = "";
  formData.problemTime = "";
  formData.problemUserId = "";
  formData.problemUserName = "";
  formData.problemTypeCode = "";
  formData.exceptionTypeSubId = "";
  formData.exceptionTypeSubCode = "";
  formData.exceptionTypeSubName = "";
  formData.exceptionLevelId = "";
  formData.problemApproachId = "";
  formData.problemApproachCode = "";
  formData.problemApproachType = "";
  formData.problemApproachName = "";
  formData.releaseState = "";
  formData.auditUserId = "";
  formData.auditUserName = "";
  formData.provisionalMeasuresDeadline = "";
  formData.rectificationDeadline = "";
  formData.rectificationRequirement = "";
  formData.agreeWith = "";
  formData.approvalOpinion = "";
  formData.approvalTime = "";
  formData.targetTime = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.qualityQuantity = 1;
  formData.occurrenceLocation = "";
  formData.productProcessContent = "";
  formData.sortName = "";
  formData.materialProcessCode = "";
  formData.materialProcessName = "";
  formData.materialProcessId = "";
  formData.materialProcessFigure = "";
  formData.exceptionSolvingType = "";
  formData.exceptionLevelCode = "";
  formData.exceptionLevel = "";
  formData.exceptionLevelContent = "";

  formData.q8dExceptionAssignDTO = JSON.parse(
    JSON.stringify(q8dExceptionAssignDTO)
  );
}
const q8dExceptionAssignDTO = {
  id: "",
  qualityExceptionId: "",
  qualityExceptionCode: "",
  exceptionAssignCode: "",
  customerComplaintCode: "",
  startDate: "",
  contentTypeId: "",
  contentType: "",
  propertySortId: "",
  propertySort: "",
  supplyId: "",
  supplyCode: "",
  supplyName: "",
  materialId: "",
  materialCode: "",
  materialName: "",
  figureNumber: "",
  exceptionQuantity: 1,
  exceptionDescription: "",
  exceptionAssignState: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
  assetPhotos: [],
};
const loading = ref(false);

// /** 质量异常信息保存提交 */
// function handleSubmit() {
//   formRef.value.validate(async (valid: any) => {
//     if (valid) {
//       loading.value = true;
//       const text = formData.id ? "编辑" : "新增";
//       await request({
//         url: "/qms/q8dqualityproblem/save",
//         method: "post",
//         data: formData,
//       }).finally(() => {
//         loading.value = false;
//       });
//       ElMessage.success(text + `成功`);
//       closeDialog();
//       emit("reset-table", formData.id);
//     }
//   });
// }

// // 产品过程状态列表
// const materialProcessStateList = ref([]);
// const getMaterialProcessStateList = async () => {
//   let res = await request({
//     url: "/qms/qmsmaterialprocessstate/getList",
//     method: "post",
//     data: {},
//   }).finally(() => {
//     loading.value = false;
//   });
//   console.log("产品状态", res.data);

//   materialProcessStateList.value = res.data;
// };

// // 改变产品状态
// const handleChangeMaterialProcessState = (e) => {
//   if (e) {
//     console.log(e);
//   }
// };

// // 供应商列表
// const supplyList = ref([]);
// async function getSupplyList() {
//   let res = await request({
//     url: "mdm/mdmpartner/getListByFull",
//     method: "post",
//     data: {},
//   }).finally(() => {
//     loading.value = false;
//   });
//   supplyList.value = res.data;
// }

// const changSupplyList = (e) => {
//   if (e) {
//     supplyList.value.forEach((item) => {
//       if (item.id == e) {
//         formData.supplyName = item.partnerName;
//         formData.supplyCode = item.partnerCode;
//       }
//     });
//   } else {
//     formData.supplyName = "";
//     formData.supplyCode = "";
//   }
// };

// const changIsInternal = (e) => {
//   console.log(e);
//   formData.supplyId = "";
//   formData.supplyName = "";
//   formData.supplyCode = "";
// };

// 责任部门

const organizationList = ref([]);
const getOrganizationList = async () => {
  let res = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 4 },
  }).finally(() => {
    loading.value = false;
  });
  organizationList.value = res.data;
  console.log("organizationList", organizationList.value);
};

const handleChangeDepartment = (e) => {
  if (e) {
    formData.departmentName = e.organizationName;
    formData.departmentCode = e.organizationCode;
  } else {
    formData.departmentName = "";
    formData.departmentCode = "";
  }
};

// // 失效模式树列表
// const failureModeList = ref([]);
// async function getFailureMode() {
//   let res = await request({
//     url: "/qms/qmsfailuremode/getTree",
//     method: "post",
//     data: {},
//   }).finally(() => {
//     loading.value = false;
//   });
//   failureModeList.value = res.data;
//   console.log("failureMode", failureModeList.value);
// }

// const handleNodeClick = (e) => {
//   if (e) {
//     formData.failureModeContent = e.failureModeContent;
//   } else {
//     formData.failureModeContent = "";
//   }
// };

// // 图号编号列表
// const materialProcessList = ref([]);
// async function getMaterialProcess() {
//   let res = await request({
//     url: "/qms/qmsmaterialprocess/getList",
//     method: "post",
//     data: {},
//   }).finally(() => {
//     loading.value = false;
//   });
//   materialProcessList.value = res.data;
//   console.log("materialProcessList", materialProcessList.value);
// }
// const changeFigureNumber = (e) => {
//   if (e) {
//     materialProcessList.value.forEach((item) => {
//       if (item.id == e) {
//         formData.materialProcessCode = item.materialProcessCode;
//         formData.materialProcessName = item.materialProcessName;
//         formData.materialProcessFigure = item.materialProcessFigure;
//       }
//     });
//   } else {
//     formData.materialProcessCode = "";
//     formData.materialProcessName = "";
//     formData.materialProcessFigure = "";
//   }
// };

// 问题级别
const problemLevelList = ref([]);
const getProblemLevelList = async () => {
  let res = await request({
    url: "/qms/qmsexceptionlevel/getList",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  problemLevelList.value = res.data;
  console.log("problemLevelList", problemLevelList.value);
};

// 选择问题级别

const handleChangeProblemLevel = (e) => {
  problemLevelList.value.forEach((item) => {
    if (item.id == e) {
      formData.exceptionLevelCode = item.exceptionLevelCode;
      formData.exceptionLevelContent = item.exceptionLevelContent;
      formData.exceptionLevel = item.exceptionLevelName;
    }
  });
};

// 改变问题分类获取问题处理方式类型 以及问题二级分类
const problemApproachTypeList = ref([]);

const problemTypeChange = (e) => {
  if (e == "0" || e == "1") {
    getTowProblemApproachList();
    formData.problemApproachType = "";
    if (formData.problemTypeCode == "1") {
      problemApproachTypeList.value = [{ value: "0", label: "预防纠正措施" }];
    } else {
      problemApproachTypeList.value = [
        { value: "1", label: "简单问题处理" },
        { value: "2", label: "问题8D处理" },
      ];
    }
  } else {
    towProblemApproachList.value = [];
    problemApproachTypeList.value = [];
    formData.problemApproachType = "";
  }
};
// 根据内容分类获取二级分类
const towProblemApproachList = ref([]);
const getTowProblemApproachList = async () => {
  let res = await request({
    url: "/qms/qmsexceptionsubtype/getList",
    method: "post",
    data: { exceptionTypeCode: formData.problemTypeCode },
  });
  towProblemApproachList.value = res.data;
};

// 选择二级分类
const problemTypeSubChange = (e) => {
  if (e) {
    towProblemApproachList.value.forEach((item) => {
      if (item.id == e) {
        formData.exceptionTypeSubCode = item.exceptionTypeSubCode;
        formData.exceptionTypeSubName = item.exceptionTypeSubName;
      }
    });
  } else {
    formData.exceptionTypeSubCode = "";
    formData.exceptionTypeSubName = "";
  }
};
// 下一步

const nextStep = () => {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      // 预防纠正措施弹窗
      if (formData.problemApproachType == "") {
        ElMessage.warning("请选择问题处理方式");
      } else if (formData.problemApproachType == "0") {
        MeasureItem();
      } else if (
        formData.problemApproachType == "1" ||
        formData.problemApproachType == "2"
      ) {
        FeedItem();
      }
    }
  });
};
// 预防纠正措施弹窗
const addDialogMeasure = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
/** 打开预防纠正措施弹窗表单弹窗  */
const MeasureItem = () => {
  addDialogMeasure.title = "预防纠正措施";
  // 提交预防措施 是主表内容 不能填写q8dExceptionAssignDTO所以制空
  formData.q8dExceptionAssignDTO = {};
  // 如果back1有值说明进行了上一步的操作 所以要把重新选择的数据进行赋值
  if (back1) {
    formData.departmentManagerId = back1.departmentManagerId;
    formData.departmentManagerName = back1.departmentManagerName;
    formData.provisionalMeasuresDeadline = back1.provisionalMeasuresDeadline;
    formData.rectificationDeadline = back1.rectificationDeadline;
    formData.rectificationRequirement = back1.rectificationRequirement;
  }
  addDialogMeasure.data = formData;
  addDialogMeasure.visible = true;
};
/**  预防纠正措施弹窗点击取消按钮 和关闭弹窗返回的数据 */
function changeVisibleMeasure(value: boolean, backData: any) {
  if (addDialogMeasure.visible === true) {
    addDialogMeasure.visible = value;
    console.log("backData", backData);
    if (!value) {
      addDialogMeasure.data = {};
    }
  }
}
/**  预防纠正措施弹窗点击取消上一步 带回已填的数据 */
let back1 = {};
function backList(value: boolean, backData: any) {
  addDialogMeasure.visible = value;
  back1 = backData;
  console.log("backData1", back1);
  if (!value) {
    addDialogMeasure.data = {};
  }
}
/**  预防纠正措施弹窗点击取消提交后 关闭所有弹窗 */
function goback(value: boolean) {
  addDialogMeasure.visible = value;
  if (!value) {
    addDialogMeasure.data = {};
    closeDialog();
    emit("reset-table", formData.id);
  }
}

// 问题处理方式
const handleChangeProblemApproachType = () => {};

// 反馈8D弹窗
const addDialogFeed = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
/** 质量问题信息表单弹窗表单弹窗  */
const FeedItem = () => {
  // 如果back2有值说明进行了上一步的操作 所以要把重新选择的数据进行赋值
  console.log(formData);
  console.log(back2);

  if (back2 != null) {
    formData.q8dExceptionAssignDTO = Object.assign(
      formData.q8dExceptionAssignDTO,
      back2.q8dExceptionAssignDTO
    );
    console.log(formData.q8dExceptionAssignDTO);
  }
  console.log("formData.q8dExceptionAssignDTO", formData.q8dExceptionAssignDTO);
  // 判断是否是保存之后进来的页面 保存之后有id 没有id是新增再把数据赋值到相应字段
  if (formData.q8dExceptionAssignDTO == null) {
    formData.q8dExceptionAssignDTO = JSON.parse(
      JSON.stringify(q8dExceptionAssignDTO)
    );
    formData.q8dExceptionAssignDTO.qualityExceptionCode =
      formData.qualityProblemCode;
    formData.q8dExceptionAssignDTO.supplyId = formData.supplyId;
    formData.q8dExceptionAssignDTO.supplyName = formData.supplyName;
    formData.q8dExceptionAssignDTO.supplyCode = formData.supplyCode;
    formData.q8dExceptionAssignDTO.materialId = formData.materialProcessId;
    formData.q8dExceptionAssignDTO.materialCode = formData.materialProcessCode;
    formData.q8dExceptionAssignDTO.materialName = formData.materialProcessName;
    formData.q8dExceptionAssignDTO.figureNumber =
      formData.materialProcessFigure;
    formData.q8dExceptionAssignDTO.exceptionQuantity = formData.qualityQuantity;
    formData.q8dExceptionAssignDTO.startDate = `${dayjs(new Date()).format(
      "YYYY-MM-DD"
    )}`;
  }
  addDialogFeed.title =
    formData.problemApproachType == "1"
      ? "问题简单反馈报告分派"
      : "8D问题反馈报告";

  console.log(formData.q8dExceptionAssignDTO);

  addDialogFeed.data = formData;
  addDialogFeed.visible = true;
  addDialogFeed.visible = true;
};
/** 质量问题信息表单弹窗 点击取消和关闭弹窗*/
function changeVisibleFeed(value: boolean) {
  addDialogFeed.visible = value;
  back2 = null;
  formData.q8dExceptionAssignDTO = null;
  if (!value) {
    addDialogFeed.data = {};
  }
}
/** 质量问题信息表单弹窗点击取消上一步 带回已填的数据 */
let back2 = null;
function backList2(value: boolean, backData: any) {
  addDialogFeed.visible = value;
  back2 = backData;
  console.log("backData2", back2);
  if (!value) {
    addDialogFeed.data = {};
  }
}
/**  质量问题信息窗点击取消提交后 关闭所有弹窗 */
function goback2(value: boolean) {
  addDialogFeed.visible = value;
  if (!value) {
    addDialogFeed.data = {};
  }
  closeDialog();
  emit("reset-table", formData.id);
}

onMounted(() => {
  // getFailureMode();
  // getMaterialProcessStateList();
  getOrganizationList();
  // getSupplyList();
  // getMaterialProcess();
  getProblemLevelList();
});
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
